function onSelectCourse() {
	var score_type = $('#__enter_course').find('input[name="score_type"]:checked').val();
	if(score_type == null) {
		score_type = 1;
	}
	if(score_type == 2) {
		$('#__enter_course').find('#__holes_played').show();
	} else {
		$('#__enter_course').find('#__holes_played').hide();
	}
	var golf_course_id = $('#__enter_course').find('#__courses').val();
	if(golf_course_id == 0) {
		$('#__course_detail_temp').show();
		$('#__course_detail').hide();
		$('#__course_detail').html('');
		$('#__rating').val('');
		$('#__slope').val('');
		return;
	}
	var tee = $('#__enter_course').find('#__tee').val();
	if(tee == null) {
		tee = $('#__enter_course').find('#__tee_temp').val();
		if(tee == null) {
			tee = 'gold';
		}
	}
	var golf_score_id = $('#__enter_course').find('#__golf_score_id').val();
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/fetch_course" ?>',
		dataType: "html",
		cache: false,
		data: {
			'golf_course_id': golf_course_id,
			'score_type': score_type,
			'tee': tee,
			'golf_score_id' : golf_score_id
		},
		success: function(data) {
			//console.log(data);
			$('.loading').hide();
			$('#__course_detail').html(data);
			$('#__course_detail_temp').hide();
			$('#__course_detail').show();
		}
	});
}
function onFinishInputScore(hole) {
	var scoreTotal = 0; 
	var outTotal = 0;
	var inTotal = 0;
	for(var i = 1; i <= 18; i++) {
		var score = Number($('#__enter_course').find('#__score_' + i + '_input').val());
		scoreTotal += score;
		if(i <= 9) {
			outTotal += score;
		} else {
			inTotal += score;
		}
	} 
	$('#__enter_course').find('#__score_total').html(scoreTotal);
	$('#__enter_course').find('#__score_out').html(outTotal);
	$('#__enter_course').find('#__score_in').html(inTotal);
}


function onViewCourse(cid,cname) {
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/scores" ?>',
		dataType: "html",
		cache: false,
		data: {
			'cid': cid,
		},
		success: function(data) {
			//console.log(data);
			$('.loading').hide();
			$('.title-top').text(cname);
			changeTitle({'tcourse':cname});
			changePage(VIEW_SCORE,data);
		}
	});
}
function onMapCourse() {
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/map_courses" ?>',
		dataType: "html",
		cache: false,
		data: {
			//'cid': cid,
		},
		success: function(data) {
			//console.log(data);
			$('.loading').hide();
			//$('.title-top').text(cname);
			//changeTitle({'tcourse':cname});
			//active_link(l-viewcourse);
			changePage(VIEW_MAP_COURSE,data);
		}
	});
}

function onMapHole(cid,cname){
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/map_hole" ?>',
		dataType: "html",
		cache: false,
		data: {
			cid: cid,
		},
		success: function(data) {
			//console.log(data);
			$('.loading').hide();
			$('.title-top').text(cname);
			changeTitle({'tcourse':cname});
			//active_link(l-viewcourse);
			changePage(VIEW_HOLE,data);
		}
	});
}

function onMapHoleDetail(golf_course_id,hole,tee){
	$('.loading').show();
	$.ajax({type: "POST",
		//url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/map_hole_detail" ?>',
		url: '<?php echo elgg_get_site_url() . (isadminloggedin() ? "ajax/view/golfscore/admin_map_hole_detail" : "ajax/view/golfscore/map_hole_detail") ?>',
		dataType: "html",
		cache: false,
		data: {
			golf_course_id:golf_course_id,
			hole:hole,
			tee: tee,
		},
		success: function(data) {
			//console.log(data);
			$('.loading').hide();
			//$('.title-top').text(cname);
			//changeTitle({'tcourse':cname});
			//active_link(l-viewcourse);
			changePage(VIEW_HOLE_DETAIL,data);
		}
	});
}

function onViewUserScores(params) {
	var cid = params['cid']? params['cid'] : ''; 
	var uid = params['uid']? params['uid'] : '';
	
	var uname = params['uname']? params['uname'] : '';
	var cname = params['cname']? params['cname'] : '';
	if(uid.length == 0) return;
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/user_scores" ?>',
		dataType: "html",
		cache: false,
		data: {
			'cid': cid,
			'uid' : uid,
		},
		success: function(data) {
			$('.loading').hide();	
			//changeTitle({'uname':'<?php echo $golf_score->user_full_name ?>'});
			changePage(VIEW_USER_SCORE,data);
		}
	});
}

function onViewScore(scoreid) {
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/score_detail" ?>',
		dataType: "html",
		cache: false,
		data: {
			'scoreid': scoreid,
		},
		success: function(data) {
			//console.log(data);
			//$('.title-top').text($('.title-top').text()+"\\"+ "<?php echo $golf_score->user_full_name;?>");
			//changeTitle({'tcourse':'<?php echo $golf_score->course_name ?>','uname':'<?php echo $golf_score->user_full_name ?>'});
			$('.loading').hide();
			changePage(VIEW_SCORE_DETAIL,data);
		}
	});
}

function onEnterScore() {
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/add" ?>',
		dataType: "html",
		cache: false,
		data: {
		},
		success: function(data) {
			$('.loading').hide();
			changePage(VIEW_ENTER_SCORE,data);
		}
	});
}

function onLogin(){
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/login" ?>',
		dataType: "html",
		cache: false,
		data: {
		},
		success: function(data) {
			$('.loading').hide();
			changePage(VIEW_LOGIN,data);
		}	
	});
}

function onEditScore(golf_score_id,hole,tee) {
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/edit" ?>',
		dataType: "html",
		cache: false,
		data: {
			golf_score_id:golf_score_id,
			hole:hole,
			tee : tee,
		},
		success: function(data) {	
			//changePage(VIEW_ENTER_SCORE,data);
			$.ajax({type: "POST",
				url: '<?php echo elgg_get_site_url() . "ajax/view/golfscore/map_hole_detail" ?>',
				dataType: "html",
				cache: false,
				data: {
					golf_score_id: golf_score_id,
					hole: hole,
					tee: tee,
				},
				success: function(data1) {
					$('.loading').hide();
					changePage(EDIT_HOLEDETAIL,data1+data);
				}
			});	
		}
	});
	
	
	
}

function onSaveScoreForm(scoreid,hole){
	$('.loading').show();
	$.ajax(
	{
		url: '<?php echo elgg_get_site_url() . "action/golfscore/save" ?>',
		type: 'post',
		dataType: 'json',
		data : $('#__enter_score_form').serialize(),
		success: function (data){
			$('.loading').hide();
			if(data){
				if(data.status == -1){
					alert(data.system_messages.error);
				} else {
					if(scoreid){
						if (hole){
							hole = parseInt(hole);
							hole = hole == 18 ? 1 : hole+1;
							var tee = $('#__tee').val();
							onEditScore(scoreid,hole);
						} else {
							onViewScore(scoreid);
						}
					} else {
						//var params ={
						//				'uid':"<?php echo get_loggedin_userid();?>",
						//				'cid':data.output,
						//				'uname':"",
						//				'cname':"",
						//			};
						if(data.output){
							if (hole){
								hole = parseInt(hole);
								hole = hole == 18 ? 1 : hole+1;
								var tee = $('#__tee').val();
								onEditScore(data.output,hole);
							} else {
								onViewScore(data.output);
							}
						}
					
						//onViewScore(data.output);
					}
				}
			}
			
		}	 
	});
}

function onNextHole(golf_score_id,hole){
	if (hole){
							hole = parseInt(hole);
							hole = hole == 18 ? 1 : hole + 1;
							onEditScore(golf_score_id,hole);		
	}
}

function onLastHole(golf_score_id,hole){
	if (hole){
							hole = parseInt(hole);
							hole = hole == 18 ? 1 : hole - 1;
							onEditScore(golf_score_id,hole);				
	} 
}

function onNextHoleCourse(golf_course_id,hole){
	if (hole){
							hole = parseInt(hole);
							hole = hole == 18 ? 1 : hole + 1;
							onMapHoleDetail(golf_course_id,hole);		
	}
}

function onLastHoleCourse(golf_course_id,hole){
	if (hole){
							hole = parseInt(hole);
							hole = hole == 1 ? 18 : hole - 1;
							onMapHoleDetail(golf_course_id,hole);				
	} 
}

function onSaveScore(golf_score_id,score_type,holes_played,month,day,year,course_id,tee,rating,slope){
	<?php   
		$__elgg_ts = time();
		$__elgg_token = generate_action_token($__elgg_ts); 
	?>;
	
	var ts = '<?php echo $__elgg_ts;?>';
	var tk = '<?php echo $__elgg_token;?>';
	$('#save_score_error_id').hide();
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "action/golfscore/save" ?>',
		dataType: "json",
		cache: false,
		data: {
			'golf_score_id':golf_score_id,
			'score_type':score_type,
			'holes_played':holes_played,
			'month':month,
			'day':day,
			'year':year,
			'course_id':course_id,
			'tee':tee,
			'rating':rating,
			'slope':slope,
			'score':$('#score_id').val(),
			'__elgg_token': tk,
			'__elgg_ts': ts,
		},
		success: function(data) {
			$('.loading').hide();
			if(data){
				if(data.status == -1){
					alert(data.system_messages.error);
				} else {
					//Reload parent view
				}
			}
		}
	});
}

function onViewMap(params){
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/igolf_map/map" ?>',
		dataType: "html",
		cache: false,
		data: {
			hole:params['hole'],
		},
		success: function(data) {
			changePage(VIEW_MAP,data);
		}
	});
}
function onSaveGolfCourseCard(golf_course_card_id,golf_course_id,hole,gold_tee_distance,blue_tee_distance,white_tee_distance,
								red_tee_distance,par,hole_x,hole_y,gold_tee_x,gold_tee_y,blue_tee_x,blue_tee_y,
								white_tee_x,white_tee_y,red_tee_x,red_tee_y){
	<?php   
		$__elgg_ts = time();
		$__elgg_token = generate_action_token($__elgg_ts); 
	?>;
	
	var ts = '<?php echo $__elgg_ts;?>';
	var tk = '<?php echo $__elgg_token;?>';
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "action/golfscore/save_course_card" ?>',
		dataType: "json",
		cache: false,
		data: {
			'golf_course_card_id' : golf_course_card_id,
			'gold_tee_distance' : gold_tee_distance,
			'blue_tee_distance' : blue_tee_distance,
			'white_tee_distance' : white_tee_distance,
			'red_tee_distance' : red_tee_distance,
			'par' : par,
			'hole_x' : hole_x,
			'hole_y' : hole_y,
			'gold_tee_x' : gold_tee_x,
			'gold_tee_y' : gold_tee_y,
			'blue_tee_x' : blue_tee_x,
			'blue_tee_y' : blue_tee_y,
			'white_tee_x' : white_tee_x,
			'white_tee_y' : white_tee_y,
			'red_tee_x' : red_tee_x,
			'red_tee_y' : red_tee_y,
			'__elgg_token': tk,
			'__elgg_ts': ts,
		},
		success: function(data) {
			$('.loading').hide();
			if(data){
				if(data.output.error != '0'){
					//alert(data.system_messages.error);
				} else {
					onMapHoleDetail(golf_course_id,hole);
				}
			}
		},
		error : function(xhr, ajaxOptions, thrownError) {
			$('.loading').hide();
			//alert(xhr.status);
			//alert(thrownError);
		},
	});
}

function onAddCourse() {
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "ajax/view/forms/golfscore/save_course" ?>',
		dataType: "html",
		cache: false,
		success: function(data) {
			changePage(VIEW_COURSE,data);
		}
	});
}
function onChangeTee(tee_name) {
	var tee_container = $('#__course_form').find('#' + tee_name);
	var tee = $(tee_container).find('#__tee_name').val();
	$(tee_container).find('#__tee_color').removeClass();
	$(tee_container).find('#__tee_color').addClass('elgg-golfscore-icon-tee-' + tee);
}
function onSaveGolfCourse(golf_course_id,course_name,par_total,
							gold_tee_name,gold_course_rating,gold_course_slope,gold_total,
							blue_tee_name,blue_course_rating,blue_course_slope,blue_total,
							white_tee_name,white_course_rating,white_course_slope,white_total,
							red_tee_name,red_course_rating,red_course_slope,red_total){
	<?php   
		$__elgg_ts = time();
		$__elgg_token = generate_action_token($__elgg_ts); 
	?>;
	
	var ts = '<?php echo $__elgg_ts;?>';
	var tk = '<?php echo $__elgg_token;?>';
	$('.loading').show();
	$.ajax({type: "POST",
		url: '<?php echo elgg_get_site_url() . "action/golfscore/save_course" ?>',
		dataType: "json",
		cache: false,
		data: {
			'golf_course_id' : golf_course_id,
			'course_name' : course_name,
			'par_total' : par_total,
			'gold_tee_name' : gold_tee_name,
			'gold_course_rating' : gold_course_rating,
			'gold_course_slope' : gold_course_slope,
			'gold_total' : gold_total,
			'blue_tee_name' : blue_tee_name,
			'blue_course_rating' : blue_course_rating,
			'blue_course_slope' : blue_course_slope,
			'blue_total' : blue_total,
			'white_tee_name' : white_tee_name,
			'white_course_rating' : white_course_rating,
			'white_course_slope' : white_course_slope,
			'white_total' : white_total,
			'red_tee_name' : red_tee_name,
			'red_course_rating' : red_course_rating,
			'red_course_slope' : red_course_slope,
			'red_total' : red_total,
			'__elgg_token': tk,
			'__elgg_ts': ts,
		},
		success: function(data) {
			$('.loading').hide();
			if(data){
				if(data.output.error != '0'){
					//alert(data.system_messages.error);
				} else {
					onMapCourse();
				}
			}
		},
		error : function(xhr, ajaxOptions, thrownError) {
			$('.loading').hide();
			//alert(xhr.status);
			//alert(thrownError);
		},
	});
}